----------------------------------------------------------------------------------------------------------------------------------------------------
--[[

梅花 - Plum blossom         plum_blossom
煤花 - Coal flower           coal_flower
没花 - No flower            no_flower   
莓花 - Berry flower         berry_flower

梅花，可食用，保质期7天，最多叠加7个。腐烂后变为1块煤炭，一个噩梦燃料，一个多汁浆果。

食用后回复7饱度7理智7血，可以下锅，0.5果度，0.5菜度。

可作为燃料，相当于煤炭（因为是mei花），可以放进盐盒。

]]--
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 素材
    local assets = {
        Asset("ANIM", "anim/hutao_plants_plum_blossom_flower.zip"), 
        -- Asset( "IMAGE", "images/inventoryimages/hutao_cooked_food_excample.tex" ),
        -- Asset( "ATLAS", "images/inventoryimages/hutao_cooked_food_excample.xml" ),
    }
----------------------------------------------------------------------------------------------------------------------------------------------------
---
    local all_prefabs = {}
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 种植
    local function blossom_petal_OnDeploy(inst, pt, deployer)
        local flower = SpawnPrefab("planted_flower")
        if flower then
            if flower.components.hutao_com_flower_switcher then
                if TheWorld.state.issummer then
                    flower.components.hutao_com_flower_switcher:Switch("no_flower")
                else
                    flower.components.hutao_com_flower_switcher:Switch("plum_blossom")
                end
            end
            flower.Transform:SetPosition(pt.x,0,pt.z)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 物品
    local item_data = {
        -------------------------------------------------------
        --- 梅花
            ["hutao_item_plum_blossom_petal"] = {
                ["bank"] = "hutao_plants_plum_blossom_flower",
                ["build"] = "hutao_plants_plum_blossom_flower",
                ["anim"] = "plum_blossom_petal",
                ["imagename"] = "hutao_item_plum_blossom_petal",
                ["atlasname"] = "images/inventoryimages/hutao_item_plum_blossom_petal.xml",
                ["fn"] = function(inst)
                    inst.components.perishable.onperishreplacement = "spoiled_food" -- 腐烂后变成腐烂食物                    
                    inst:AddComponent("deployable")
                    inst.components.deployable.ondeploy = blossom_petal_OnDeploy
                    inst.components.deployable:SetDeployMode(DEPLOYMODE.PLANT)
                    inst.components.deployable:SetDeploySpacing(DEPLOYSPACING.LESS)
                end,
            },
        -------------------------------------------------------
        --- 煤花
            ["hutao_item_coal_flower_petal"] = {
                ["bank"] = "hutao_plants_plum_blossom_flower",
                ["build"] = "hutao_plants_plum_blossom_flower",
                ["anim"] = "coal_flower_petal",
                ["imagename"] = "hutao_item_coal_flower_petal",
                ["atlasname"] = "images/inventoryimages/hutao_item_coal_flower_petal.xml",
                ["fn"] = function(inst)
                    inst.components.perishable.onperishreplacement = "charcoal" -- 腐烂后变成腐烂食物                
                end,
            },
        -------------------------------------------------------
        --- 没花
            ["hutao_item_no_flower_petal"] = {
                ["bank"] = "hutao_plants_plum_blossom_flower",
                ["build"] = "hutao_plants_plum_blossom_flower",
                ["anim"] = "no_flower_petal",
                ["imagename"] = "hutao_item_no_flower_petal",
                ["atlasname"] = "images/inventoryimages/hutao_item_no_flower_petal.xml",
                ["fn"] = function(inst)
                    inst.components.perishable.onperishreplacement = "nightmarefuel" -- 腐烂后变成腐烂食物                
                end,
            },
        -------------------------------------------------------
        --- 莓花
            ["hutao_item_berry_flower_petal"] = {
                ["bank"] = "hutao_plants_plum_blossom_flower",
                ["build"] = "hutao_plants_plum_blossom_flower",
                ["anim"] = "berry_flower_petal",
                ["imagename"] = "hutao_item_berry_flower_petal",
                ["atlasname"] = "images/inventoryimages/hutao_item_berry_flower_petal.xml",
                ["fn"] = function(inst)
                    inst.components.perishable.onperishreplacement = "berries_juicy" -- 腐烂后变成腐烂食物                
                end,
            },
        -------------------------------------------------------
    }
    local function item_common()

        local inst = CreateEntity()
        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddNetwork()

        MakeInventoryPhysics(inst)

        -- inst.AnimState:SetBank("hutao_cooked_food_excample")
        -- inst.AnimState:SetBuild("hutao_cooked_food_excample")
        -- -- inst.AnimState:PlayAnimation("idle")
        inst.pickupsound = "vegetation_grassy"
        inst:AddTag("saltbox_valid")
        
        MakeInventoryFloatable(inst)

        inst.entity:SetPristine()
        
        if not TheWorld.ismastersim then
            return inst
        end
        --------------------------------------------------------------------------
        -- 物品名 和检查文本
            inst:AddComponent("inspectable")
            inst:AddComponent("inventoryitem")
            -- inst.components.inventoryitem.imagename = "hutao_cooked_food_excample"
            -- inst.components.inventoryitem.atlasname = "images/inventoryimages/hutao_cooked_food_excample.xml"
        --------------------------------------------------------------------------
        -- 可食用
            inst:AddComponent("edible") -- 可食物组件
            inst.components.edible.foodtype = FOODTYPE.VEGGIE
            -- inst.components.edible:SetOnEatenFn(function(inst,eater)
            --     if eater and eater:HasTag("player") then

            --     end
            -- end)
            inst.components.edible.hungervalue = 7
            inst.components.edible.sanityvalue = 7
            inst.components.edible.healthvalue = 7
        --------------------------------------------------------------------------
        -- 可腐烂
            inst:AddComponent("perishable")
            inst.components.perishable:SetPerishTime(TUNING.PERISH_ONE_DAY*7)
            inst.components.perishable:StartPerishing()
            inst.components.perishable.onperishreplacement = "spoiled_food" -- 腐烂后变成腐烂食物
        --------------------------------------------------------------------------
        -- 可堆叠
            inst:AddComponent("stackable")
            inst.components.stackable.maxsize = TUNING.STACK_SIZE_LARGEITEM
        --------------------------------------------------------------------------
            MakeHauntableLaunch(inst)
        -------------------------------------------------------------------
        --- 落水影子
            -- local function shadow_init(inst)
            --     if inst:IsOnOcean(false) then       --- 如果在海里（不包括船）
            --         inst.AnimState:Hide("SHADOW")
            --     else                                
            --         inst.AnimState:Show("SHADOW")
            --     end
            -- end
            -- inst:ListenForEvent("on_landed",shadow_init)
            -- shadow_init(inst)
        -------------------------------------------------------------------
        --- 燃料、引燃
            inst:AddComponent("fuel")
            inst.components.fuel.fuelvalue = TUNING.MED_FUEL
            MakeMediumBurnable(inst, TUNING.MED_BURNTIME)
            MakeMediumPropagator(inst)
        -------------------------------------------------------------------
        
        return inst
    end
    for prefab, data in pairs(item_data) do
        local function temp_fn()
            local inst = item_common()
            inst.AnimState:SetBank(data.bank)
            inst.AnimState:SetBuild(data.build)
            inst.AnimState:PlayAnimation(data.anim)
            if not TheWorld.ismastersim then
                return inst
            end
            inst.components.inventoryitem.imagename = data.imgename
            inst.components.inventoryitem.atlasname = data.atlasname
            if data.fn then
                data.fn(inst)
            end
            return inst
        end
        table.insert(all_prefabs, Prefab(prefab, temp_fn, assets))
        --- 设置可以放烹饪锅里   0.5果度，0.5菜度。
        AddIngredientValues({prefab}, { 
            fruit = 0.5,
            veggie = 0.5,
        })
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 冷源热源
    local function find_firesuppressor(target,inst) --- 雪球机
        return target.prefab == "firesuppressor"
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 说话函数
    local function flower_kill_talk_sound(inst)
        inst.SoundEmitter:KillSound("talk")        
    end
    local function flower_say_by_index(inst,index)
        inst.components.hutao_com_dialogue:Say(index)
        inst.SoundEmitter:PlaySound("dontstarve/characters/woodie/lucytalk_LP", "talk")
        inst:DoTaskInTime(2.5,flower_kill_talk_sound)
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 花朵计时器函数
    local ONE_DAY_TIME = 480 -- 一天有480秒
    local TEMPERATURE_SOURCE_RADIUS = 7 -- 温度源半径
    local function _coal_flower_timer_fn(inst)
        --- 存在一天后会自燃，被附近有冷源或被雪球机降温或变得潮湿可重置这一进程。
        local auto_burnt_time = inst.components.hutao_com_flower_switcher:Add("auto_burnt_time", 1)
        if auto_burnt_time >= ONE_DAY_TIME and not (inst.components.burnable:IsBurning() or inst.components.burnable:IsSmoldering()) then
            inst.components.hutao_com_flower_switcher:Set("auto_burnt_time",0)
            if not ( inst:GetIsWet() or TheWorld.state.israining 
                or inst.components.hutao_com_flower_switcher:IsNearColdSource(TEMPERATURE_SOURCE_RADIUS)
                or inst.components.hutao_com_flower_switcher:IsNearColdSource(TUNING.FIRE_DETECTOR_RANGE,find_firesuppressor)
                )then                
                inst.components.burnable:Ignite()
            end
        end
            -- 【煤花-花朵】自燃前的半天，每隔半分钟会说一次话：回忆起来了，这种燃烧的感觉/全空间演算完成，燃烧开始
            if auto_burnt_time >= ONE_DAY_TIME/2 and auto_burnt_time % 30 == 0 then
                flower_say_by_index(inst,"flower.coal_flower_auto_burnt_warning")
            end
        --- 【煤花-花朵】在夏季以外3天后会变为【梅花-花朵】。        
        if TheWorld.state.issummer then
            inst.components.hutao_com_flower_switcher:Set("auto_trans_time",0)
        elseif inst.components.hutao_com_flower_switcher:Add("auto_trans_time",1) >= 3*ONE_DAY_TIME then
            inst.components.hutao_com_flower_switcher:Switch("plum_blossom")
        end
    end
    local function _no_flower_timer_fn(inst)
        --- 【没花-花朵】存在三天后会自然消失，附近有热源或没花变得潮湿会重置这一进程。
        local auto_remove_time = inst.components.hutao_com_flower_switcher:Add("auto_remove_time", 1)
        if auto_remove_time >= ONE_DAY_TIME*3 then
            inst.components.hutao_com_flower_switcher:Set("auto_remove_time",0)
            if not( inst:GetIsWet() or TheWorld.state.israining 
                or inst.components.hutao_com_flower_switcher:IsNearHotSource(TEMPERATURE_SOURCE_RADIUS)  )  then
                inst:Remove()
                return
            end
        end
            --- 超过2天,每隔一分钟会说一次话：不要，不要忘记梅儿/梅儿，会消失吗……
            if auto_remove_time >= ONE_DAY_TIME*2 and auto_remove_time%60 == 0 then
                flower_say_by_index(inst,"flower.no_flower_remove_warning")
            end
        --- 【没花-花朵】在冬季以外1天后会变为【梅花-花朵】。
        if TheWorld.state.iswinter then
            inst.components.hutao_com_flower_switcher:Set("auto_trans_time",0)
        elseif inst.components.hutao_com_flower_switcher:Add("auto_trans_time",1) >= ONE_DAY_TIME then
            inst.components.hutao_com_flower_switcher:Switch("plum_blossom")
        end
        
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 花朵(种植在地面上的)
    local flower_data = {
        ["hutao_plant_plum_blossom"] = "plum_blossom",  -- 梅花
        ["hutao_plant_berry_flower"] = "berry_flower",  -- 莓花

        -- ["hutao_plant_coal_flower"] = "coal_flower",
        -- ["hutao_plant_no_flower"] = "no_flower",

        ["hutao_plant_coal_flower"] = function(inst)  -- 煤花
            --------------------------------------------------------------------------------------------------------
            --- 动画
                inst.AnimState:SetBank("hutao_plants_plum_blossom_flower")
                inst.AnimState:SetBuild("hutao_plants_plum_blossom_flower")
                inst.AnimState:PlayAnimation("coal_flower")
            --------------------------------------------------------------------------------------------------------
            --- 辅助
                inst:SetDeploySmartRadius(DEPLOYSPACING_RADIUS[DEPLOYSPACING.LESS] / 2) --butterfly deployspacing/2
                inst:AddTag("hutao_tag.coal_flower")
            --------------------------------------------------------------------------------------------------------
            --- 施肥.
                inst:ListenForEvent("HUTAO_OnEntityReplicated.hutao_com_acceptable",function(inst,replica_com)
                    replica_com:SetTestFn(function(inst,item,doer,right_click)
                        if item and item.prefab == "ice" then
                            return true
                        end
                        return false
                    end)
                    replica_com:SetText("hutao_plant_coal_flower",STRINGS.ACTIONS.FERTILIZE)
                end)
                if TheWorld.ismastersim then
                    inst:AddComponent("hutao_com_acceptable")
                    inst.components.hutao_com_acceptable:SetOnAcceptFn(function(inst,item,doer)
                        item.components.stackable:Get():Remove()
                        local x,y,z = inst.Transform:GetWorldPosition()

                        local flower = SpawnPrefab("flower")
                        if flower.components.hutao_com_flower_switcher then
                            flower.components.hutao_com_flower_switcher:Switch("plum_blossom")
                        end
                        flower.Transform:SetPosition(x,y,z)
                        SpawnPrefab("halloween_firepuff_cold_"..math.random(3)).Transform:SetPosition(x,y,z)
                        inst:Remove()
                        return true
                    end)
                end
            --------------------------------------------------------------------------------------------------------
            --- 采集
                inst:ListenForEvent("HUTAO_OnEntityReplicated.hutao_com_workable",function(inst,replica_com)
                    replica_com:SetTestFn(function(inst,doer,right_click)
                        return true
                    end)
                    replica_com:SetText("hutao_plant_coal_flower",STRINGS.ACTIONS.PICK.GENERIC)
                    replica_com:SetSGAction("doshortaction")
                end)
                if TheWorld.ismastersim then
                    inst:AddComponent("hutao_com_workable")
                    inst.components.hutao_com_workable:SetOnWorkFn(function(inst,doer)
                        -- 【煤花-花朵】直接采摘的话，25%会获得【梅花-花瓣】，75%会获得7个煤炭。
                        if doer.components.inventory then
                                if math.random(1000)/10 < 25 then
                                local item = SpawnPrefab("hutao_item_plum_blossom_petal")
                                doer.components.inventory:GiveItem(item)
                            else
                                local item = SpawnPrefab("charcoal")
                                item.components.stackable.stacksize = 7
                                doer.components.inventory:GiveItem(item)
                            end 
                        end
                        inst:Remove()
                        return true
                    end)
                end
            --------------------------------------------------------------------------------------------------------
            --- 计时器事件
                if TheWorld.ismastersim then
                    TheWorld.components.hutao_com_special_timer_for_theworld:AddTimer(inst,_coal_flower_timer_fn,1)
                end
            --------------------------------------------------------------------------------------------------------
            --- 可燃
                if TheWorld.ismastersim then
                    MakeMediumBurnable(inst, TUNING.MED_BURNTIME)
                    MakeMediumPropagator(inst)
                end
            --------------------------------------------------------------------------------------------------------
        end,
        ["hutao_plant_no_flower"] = function(inst)  -- 没花
            --------------------------------------------------------------------------------------------------------
            --- 动画
                inst.AnimState:SetBank("hutao_plants_plum_blossom_flower")
                inst.AnimState:SetBuild("hutao_plants_plum_blossom_flower")
                inst.AnimState:PlayAnimation("no_flower")
            --------------------------------------------------------------------------------------------------------
            --- 辅助
                inst:SetDeploySmartRadius(DEPLOYSPACING_RADIUS[DEPLOYSPACING.LESS] / 2) --butterfly deployspacing/2
                inst:AddTag("hutao_tag.no_flower")
            --------------------------------------------------------------------------------------------------------
            --- 施肥.
                inst:ListenForEvent("HUTAO_OnEntityReplicated.hutao_com_acceptable",function(inst,replica_com)
                    replica_com:SetTestFn(function(inst,item,doer,right_click)
                        if item and item.prefab == "charcoal" then
                            return true
                        end
                        return false
                    end)
                    replica_com:SetText("hutao_plant_no_flower",STRINGS.ACTIONS.FERTILIZE)
                end)
                if TheWorld.ismastersim then
                    inst:AddComponent("hutao_com_acceptable")
                    inst.components.hutao_com_acceptable:SetOnAcceptFn(function(inst,item,doer)
                        item.components.stackable:Get():Remove()
                        local x,y,z = inst.Transform:GetWorldPosition()
                        local flower = SpawnPrefab("flower")
                        if flower.components.hutao_com_flower_switcher then
                            flower.components.hutao_com_flower_switcher:Switch("plum_blossom")
                        end
                        flower.Transform:SetPosition(x,y,z)
                        SpawnPrefab("halloween_firepuff_"..math.random(3)).Transform:SetPosition(x,y,z)
                        inst:Remove()
                        return true
                    end)
                end
            --------------------------------------------------------------------------------------------------------
            --- 采集
                inst:ListenForEvent("HUTAO_OnEntityReplicated.hutao_com_workable",function(inst,replica_com)
                    replica_com:SetTestFn(function(inst,doer,right_click)
                        return true
                    end)
                    replica_com:SetText("hutao_plant_coal_flower",STRINGS.ACTIONS.PICK.GENERIC)
                    replica_com:SetSGAction("doshortaction")
                end)
                if TheWorld.ismastersim then
                    inst:AddComponent("hutao_com_workable")
                    inst.components.hutao_com_workable:SetOnWorkFn(function(inst,doer)
                        -- 【没花-花朵】直接采摘的话，有75%概率获得1朵【梅花-花瓣】和1个噩梦燃料。有25%什么也得不到（有台词）。
                        if doer.components.inventory and math.random(1000)/10 < 75 then                            
                            local item = SpawnPrefab("hutao_item_plum_blossom_petal")
                            doer.components.inventory:GiveItem(item)
                            local item = SpawnPrefab("nightmarefuel")
                            doer.components.inventory:GiveItem(item)                            
                        end
                        inst:Remove()
                        return true
                    end)
                end
            --------------------------------------------------------------------------------------------------------
            --- 计时器事件
                if TheWorld.ismastersim then
                    TheWorld.components.hutao_com_special_timer_for_theworld:AddTimer(inst,_no_flower_timer_fn,1)
                end
            --------------------------------------------------------------------------------------------------------
            --- 可燃
                if TheWorld.ismastersim then
                    MakeMediumBurnable(inst, TUNING.MED_BURNTIME)
                    MakeMediumPropagator(inst)
                end
            --------------------------------------------------------------------------------------------------------
        end,

    }
    for prefab, flower_type in pairs(flower_data) do

        local entity_replace_fn = function(inst)
            if not TheWorld.ismastersim then
                return
            end
            local x,y,z = inst.Transform:GetWorldPosition()
            inst:Remove()
            local new_flower = SpawnPrefab("flower")
            if new_flower.components.hutao_com_flower_switcher then
                new_flower.components.hutao_com_flower_switcher:Switch(flower_type)
            end
            new_flower.Transform:SetPosition(x,y,z)
        end

        local function flower_fn()
            local inst = CreateEntity()
            inst.entity:AddTransform()
            inst.entity:AddAnimState()
            inst.entity:AddNetwork()
            inst.entity:AddSoundEmitter()
            inst.entity:SetPristine()
            -------------------------------------------------------
            --- 类型
                if type(flower_type) == "string" then
                    inst:DoTaskInTime(0,entity_replace_fn)
                elseif type(flower_type) == "function" then
                    flower_type(inst)
                end
            -------------------------------------------------------
            --- 说话控制
                inst:AddComponent("talker")
                inst.components.talker.offset = Vector3(0,20,0)
                inst.components.talker.fontsize = 20
            -------------------------------------------------------
                if not TheWorld.ismastersim then
                    return inst
                end
            -------------------------------------------------------
                inst:AddComponent("hutao_com_flower_switcher")
                inst:AddComponent("hutao_com_dialogue")
            -------------------------------------------------------
            return inst
        end
        table.insert(all_prefabs, Prefab(prefab, flower_fn, assets))
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- placer
    local function placer_postinit_fn(inst)        
    end
    table.insert(all_prefabs,MakePlacer("hutao_item_plum_blossom_petal_placer", "hutao_plants_plum_blossom_flower", "hutao_plants_plum_blossom_flower", "plum_blossom", nil, nil, nil, nil, nil, nil, placer_postinit_fn, nil, nil))
----------------------------------------------------------------------------------------------------------------------------------------------------



return unpack(all_prefabs)